/**
 * @ClassName KP9_randtorandUnEqualProbability
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/10/8 16:19
 */
public class KP9_randtorandUnEqualProbability {
    /**
     * 0-1不等概率返回
     */
    public static int rand0To1UP() {
        return Math.random() < 0.76 ? 0 : 1;
    }

    /**
     * 测试0-1不等概率返回
     */
    public static void testRand0To1UP() {
        int testTimes = 1000000;
        int[] counts = new int[2];
        for (int i = 0; i < testTimes; i++) {
            int num = rand0To1UP();
            counts[num]++;
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "的次数：" + counts[i]);
        }
    }

    /**
     * 利用rand0To1UP函数生成0-1等概率返函数
     */
    public static int rand0To1() {
        int num;
        do {
            num = rand0To1UP();
        } while (num == rand0To1UP());
        return num;
    }
    /**
     * 测试0-1不等概率返回
     */
    public static void testRand0To1() {
        int testTimes = 1000000;
        int[] counts = new int[2];
        for (int i = 0; i < testTimes; i++) {
            int num = rand0To1();
            counts[num]++;
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.println(i + "的次数：" + counts[i]);
        }
    }

    public static void main(String[] args) {
        // 测试0-1不等概率
        testRand0To1UP();
        System.out.println("==============");
        // 测试0-1等概率
        testRand0To1();
        System.out.println("==============");

    }
}
